package com.qiaofang.applet.service.other.util;

import com.qiaofang.applet.service.other.domain.Corporation;
import com.qiaofang.applet.service.other.service.CorporationService;
import com.qiaofang.applet.service.other.service.DictCorpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.Iterator;

/**
 * @author: youqing.yang
 * @create: 2020/09/18 15:55
 **/
@Slf4j
@Component
public class InitFilterCondition implements InitializingBean {

    @Autowired
    private DictCorpService dictCorpService;

    @Autowired
    private CorporationService  corporationService;

    @Override
    public void afterPropertiesSet() {
        try {
            Corporation corporation = new Corporation();
            corporation.setDeleted(false);
            //所有集团
            Iterable<Corporation> iterable = corporationService.findAll(Example.of(corporation), Sort.by("createTime"));
            if (iterable == null){
                return;
            }
            Iterator<Corporation> iterator = iterable.iterator();
            while (iterator.hasNext()){
                corporation = iterator.next();
                dictCorpService.initConstanceData(corporation.getCorporationCode());
            }
        } catch (Exception e) {
            log.error("码表数据存储redis错误:{}", e);
        }
    }
}
